<?php

/**
 * @desc		数据操作接口基类
 * ---------------------------------------------------------------------
 * @author	unphp <unphp@qq.com>
 * @date		2014-03-27
 * @copyright	UnPHP 1.1
 * ---------------------------------------------------------------------
 */

namespace UnPHP\Lib\DBdriver;

class ModelSql extends CommonSql
{
    protected $_dbName = null;
    protected $_table = null;

    /**
     * 设置“私有接口”---调用的模块（服务端的模块）名称
     * @param type $dbName
     * @return ModelSql
     * @author		liuxiang <unphp@qq.com>
     * @date		2015-04-03
     */
    public function __construct($dbName)
    {
        $this->_dbName = $dbName;
        return $this;
    }

    public function getDbName()
    {
        return $this->_dbName;
    }


    /**
     * 返回一个“更新协助类”
     * @return Obj ModelSqlParseCreate
     * @author		liuxiang <unphp@qq.com>
     * @date		2015-04-03
     */
    public function createWriter()
    {
        $newdata = new ModelSqlParseCreate($this);
        return $newdata;
    }

    /**
     * 返回一个“查询解析帮助类”
     * @return Obj ModelSqlParseQuery
     * @author		liuxiang <unphp@qq.com>
     * @date		2015-04-03
     */
    public function createQuery()
    {
        $newdata = new ModelSqlParseQuery($this);
        return $newdata;
    }

    /**
     * 数据表通用接口---查询多条结果集
     * @param queryParse $parse
     * @return boolean
     * @author		liuxiang <unphp@qq.com>
     * @date		2015-04-03
     */
    public function findAll($table, ModelSqlParseQuery $parse)
    {
        $dbName = $this->getDbName();
        if (empty($table) || empty($dbName))
        {
            return FALSE;
        }
        $condition = $parse->getQueryCondition();
        $options = array();
        $sort = $parse->getQueryOrder();
        $fileds = $parse->getQueryFileds();
        $offset = $parse->getQueryOffset();
        $limit = $parse->getQueryLimit();
        if (!empty($sort))
        {
            $options['sort'] = $sort;
        }
        if (!empty($fileds))
        {
            $options['fields'] = $fileds;
        }
        if (null !== $offset && null !== $limit)
        {
            $options['offset'] = $offset;
            $options['limit'] = $limit;
        }
        return Pool::instance()
                        ->getReadPool($dbName)
                        ->conn()
                        ->findAll($table, $condition, $options);
    }

    /**
     * 数据表通用接口---查询集合总数值
     * @param queryParse $parse
     * @return boolean
     * @author		liuxiang <unphp@qq.com>
     * @date		2015-04-03
     */
    public function findCount($table, ModelSqlParseQuery $parse)
    {
        $dbName = $this->getDbName();
        if (empty($table) || empty($dbName))
        {
            return FALSE;
        }
        $condition = $parse->getQueryCondition();
        return Pool::instance()
                        ->getReadPool($dbName)
                        ->conn()
                        ->count($table, $condition);
    }

    /**
     * 数据表通用接口---查询一条结果集
     * @param queryParse $parse
     * @return boolean
     * @author		liuxiang <unphp@qq.com>
     * @date		2015-04-03
     */
    public function findOne($table, ModelSqlParseQuery $parse)
    {
        $dbName = $this->getDbName();
        if (empty($table) || empty($dbName))
        {
            return FALSE;
        }
        $condition = $parse->getQueryCondition();
        $options = array();
        $sort = $parse->getQueryOrder();
        $fileds = $parse->getQueryFileds();
        if (!empty($sort))
        {
            $options['sort'] = $parse->getQueryOrder();
        }
        if (!empty($fileds))
        {
            $options['fields'] = $parse->getQueryFileds();
        }
        return Pool::instance()
                        ->getReadPool($dbName)
                        ->conn()
                        ->findOne($table, $condition, $options);
    }

    /**
     * 数据表通用接口---更新结果集
     * @param CreateData $new
     * @return boolean
     * @author		liuxiang <unphp@qq.com>
     * @date		2015-04-03
     */
    public function update($table, ModelSqlParseCreate $parse)
    {
        $dbName = $this->getDbName();
        if (empty($table) || empty($dbName))
        {
            return FALSE;
        }
        $condition = $parse->getQueryCondition();
        $new = $parse->getUpdateData();
        return $p = Pool::instance()->getWritePool($dbName)->conn()->update($table, $condition, $new);
    }

    /**
     * 数据表通用接口---插入一条数据到集合中
     * @param CreateData $new
     * @return boolean
     * @author		liuxiang <unphp@qq.com>
     * @date		2015-04-03
     */
    public function insert($table, ModelSqlParseCreate $parse)
    {
        $dbName = $this->getDbName();
        if (empty($table) || empty($dbName))
        {
            return FALSE;
        }
        $new = $parse->getNew();
        $p = Pool::instance()->getWritePool($dbName);
        return $p->conn()->insert($table, $new);
    }

    /**
     * 数据表通用接口---删除数据
     * @param type $table
     * @param \system\lib\DBdriver\ModelSqlParseCreate $parse
     * @return boolean
     * @author		liuxiang <unphp@qq.com>
     * @date		2015-04-03
     */
    public function remove($table, ModelSqlParseCreate $parse)
    {
        $dbName = $this->getDbName();
        if (empty($table) || empty($dbName))
        {
            return FALSE;
        }
        $condition = $parse->getQueryCondition();
        return Pool::instance()->getWritePool($dbName)->conn()->remove($table, $condition);
    }

    public function errorInfo()
    {
        $dbName = $this->getDbName();
        return Pool::instance()->getWritePool($dbName)->conn()->errorInfo();
    }

}
